package com.ruoyi.web.controller.weeklyReport;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.instrument.domain.Instrument;
import com.ruoyi.sample.domain.Sample;
import com.ruoyi.sample.service.ISampleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.weeklyReport.domain.WeeklyReport;
import com.ruoyi.weeklyReport.service.IWeeklyReportService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 周报简报Controller
 *
 * @author ruoyi
 * @date 2025-05-09
 */
@RestController
@RequestMapping("/weeklyReport/weeklyReport")
public class WeeklyReportController extends BaseController
{
    @Autowired
    private IWeeklyReportService weeklyReportService;
    @Autowired
    private ISampleService sampleService;
    /**
     * 查询周报简报列表
     */
    @PreAuthorize("@ss.hasPermi('weeklyReport:weeklyReport:list')")
    @GetMapping("/list")
    public TableDataInfo list(WeeklyReport weeklyReport)
    {
        startPage();
        List<WeeklyReport> list = weeklyReportService.selectWeeklyReportList(weeklyReport);
//        for(WeeklyReport item : list){
//            Sample sample = new Sample();
//            sample.setEngineeringId(item.getEngineeringId());
//            List<Sample> sampleList = sampleService.selectSampleList(sample);
//            item.setSampleList(sampleList);
//        }
        return getDataTable(list);
    }

    /**
     * 导出周报简报列表
     */
    @PreAuthorize("@ss.hasPermi('weeklyReport:weeklyReport:export')")
    @Log(title = "周报简报", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WeeklyReport weeklyReport)
    {
        List<WeeklyReport> list = weeklyReportService.selectWeeklyReportList(weeklyReport);
        ExcelUtil<WeeklyReport> util = new ExcelUtil<WeeklyReport>(WeeklyReport.class);
        return util.exportExcel(list, "周报简报数据");
    }

    /**
     * 获取周报简报详细信息
     */
    @PreAuthorize("@ss.hasPermi('weeklyReport:weeklyReport:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(weeklyReportService.selectWeeklyReportById(id));
    }

    /**
     * 新增周报简报
     */
    @PreAuthorize("@ss.hasPermi('weeklyReport:weeklyReport:add')")
    @Log(title = "周报简报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WeeklyReport weeklyReport)
    {
        return toAjax(weeklyReportService.insertWeeklyReport(weeklyReport));
    }

    /**
     * 修改周报简报
     */
    @PreAuthorize("@ss.hasPermi('weeklyReport:weeklyReport:edit')")
    @Log(title = "周报简报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WeeklyReport weeklyReport)
    {
        weeklyReport.setUpdateType(DicConstants.BUSINESS_Manager.getCode());
        return toAjax(weeklyReportService.updateWeeklyReport(weeklyReport));
    }

    /**
     * 删除周报简报
     */
    @PreAuthorize("@ss.hasPermi('weeklyReport:weeklyReport:remove')")
    @Log(title = "周报简报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(weeklyReportService.logicDeleteWeeklyReport(ids,DicConstants.BUSINESS_Manager.getCode(),SecurityUtils.getUserId()));
    }


    /**
     * 获取周报简报详细信息
     */
    /*@PreAuthorize("@ss.hasPermi('weeklyReport:weeklyReport:query')")
    @GetMapping(value = "/getInfoFor")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(weeklyReportService.selectWeeklyReportById(id));
    }*/


    @Log(title = "导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('weeklyReport:weeklyReport:importData')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<WeeklyReport> util = new ExcelUtil<WeeklyReport>(WeeklyReport.class);
        List<WeeklyReport> weeklyReportList = util.importExcel(file.getInputStream());

        String message = weeklyReportService.importWeeklyReport(weeklyReportList);
        return AjaxResult.success(message);
    }


    @GetMapping("/importWeeklyReportTemplate")
    public AjaxResult importWeeklyReportTemplate()
    {
        ExcelUtil<Instrument> util = new ExcelUtil<Instrument>(Instrument.class);
        return util.importTemplateExcel("sheet1");
    }

}
